//
//  KLMSceneModeCell.swift
//  KLM_GN
//
//  Created by Nic on 2025/3/11.
//

import UIKit

protocol KLMSceneModeCellDelegate: AnyObject{
    func sceneModeCellTapDelete(cell: KLMSceneModeCell, index: IndexPath)
    func sceneModeCellTapEdit(cell: KLMSceneModeCell, index: IndexPath)
    func sceneModeCellLongPress(cell: KLMSceneModeCell, index: IndexPath)
}

class KLMSceneModeCell: UICollectionViewCell {
    @IBOutlet weak var bgView: UIImageView!
    @IBOutlet weak var deleteBtn: UIButton!
    @IBOutlet weak var editBtn: UIButton!
    @IBOutlet weak var titleBtn: UIButton!
    
    private var index: IndexPath = []
    weak var delegate: KLMSceneModeCellDelegate?
    
    var cellCard : KLMCard! {
        didSet {
            if cellCard.subType == .add {
                titleBtn.setImage(UIImage(named: cellCard.titleIcon ?? "add"), for: .normal)
                deleteBtn.isHidden = true
                editBtn.isHidden = true
            } else {
                titleBtn.setImage(nil, for: .normal)
                deleteBtn.isHidden = !cellCard.isDelete
                editBtn.isHidden = false
            }
            
            titleBtn.setTitle(cellCard.title, for: .normal)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        titleBtn.isUserInteractionEnabled = false
        titleBtn.titleLabel?.numberOfLines = 0
        titleBtn.configStyle(font: UIFont(safeName: "PingFangSC-Regular", size: 16), titleColor: rgba(38, 38, 38, 1))
        layer.shadowColor = rgba(0, 0, 0, 0.4).cgColor
        layer.shadowOffset = CGSize(width: 0, height: 2)
        layer.shadowOpacity = 1
        layer.shadowRadius = 6
        layer.masksToBounds = false
        
        let longPress = UILongPressGestureRecognizer.init(target: self, action: #selector(longPress))
        longPress.minimumPressDuration = 1
        self.addGestureRecognizer(longPress)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
//        if cellCard.select {
//            bgView.image = UIImage.image(withColor: appMainThemeColor, size: contentView.size, cornerRadius: 16)
//        } else {
//            bgView.image = UIImage.image(withColor: rgba(247, 247, 247, 1), size: contentView.size, cornerRadius: 16)
//        }
        if cellCard.select {
            bgView.image = UIImage.image(withColor: rgba(246, 253, 250, 1), size: bounds.size, cornerRadius: 8.0, borderColor: appMainThemeColor, borderWidth: 1)
        } else {
            bgView.image = UIImage.image(withColor: rgba(247, 247, 247, 1), size: bounds.size, cornerRadius: 16.0 )
        }
    }

    @objc func longPress(_ gesture: UILongPressGestureRecognizer) {
        
        switch gesture.state {
        case .began:
            if (self.delegate != nil) {
                self.delegate?.sceneModeCellLongPress(cell: self, index: index)
            }
        default:
            break
        }
    }
    
    static func reusableCellCell(with collectionView: UICollectionView, reuseId: String, index: IndexPath) -> Self {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseId, for: index) as? Self {
            cell.index = index
            return cell
        }
        return Self.loadFromNib()
    }
    
    @IBAction func tapEdit(_ sender: UIButton) {
        if (self.delegate != nil) {
            self.delegate?.sceneModeCellTapEdit(cell: self, index: index)
        }
    }
    @IBAction func tapDelete(_ sender: UIButton) {
        if (self.delegate != nil) {
            self.delegate?.sceneModeCellTapDelete(cell: self, index: index)
        }
    }
}
